

package com.keyinong.qudan.view;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.qudan.R;
import com.keyinong.qudan.tools.HttpTools;
import com.loopj.android.http.JsonHttpResponseHandler;

public class OrderDetailActivity extends Activity {
	WebView vb;
	//	TextView res;
	//	ImageView res_pic;
	//	ImageButton copy;
	//	ImageButton share;
	//	ImageButton browser;
	HttpTools httpTools=null;
	View iscatview = null;
	private Button btn_queren;
	private String orderid;
	private ImageButton ib_fanhui5;
	private TextView tv_cancle;
	private String state;
	private String token;
	private String paymodeid;
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_detail);
		vb=(WebView)findViewById(R.id.webView1);
		vb.getSettings().setJavaScriptEnabled(true);  
		vb.getSettings().setDomStorageEnabled(true);  
		SharedPreferences sp = getSharedPreferences("spReg",
				Context.MODE_PRIVATE);
		token = sp.getString("token", "");
		orderid = getIntent().getStringExtra("id");
//		state = getIntent().getStringExtra("state");
		
//		String url= "http://mlo.jishi88.com/index.php#!/adminorderdetail/"+orderid;
		String url= "http://mlo.jishi88.com/index.php/order/getInfo/id/"+orderid;
		vb.loadUrl(url);
		//		initView();
		btn_queren=(Button)findViewById(R.id.btn_queren);
		tv_cancle=(TextView)findViewById(R.id.tv_cancle);
		ib_fanhui5=(ImageButton)findViewById(R.id.ib_fanhui5);
		orderDetail();
		btn_queren.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
					queren();
			}
		});
		
		ib_fanhui5.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		tv_cancle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				cancledialog();
			}
		});
	}
	/**确认取餐*/
	private void queren(){
		if (httpTools == null) {
			httpTools = new HttpTools(getApplicationContext());
		}
		httpTools.yiwancheng(token, orderid,"3",paymodeid,
				new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					boolean code = response.getBoolean("code");
					String msg = response.getString("msg");
					if (code == true) {
						Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
						btn_queren.setText("订单已完成");
//						Intent intent = new Intent();
//						intent.setClass(getApplicationContext(), ScanActivity.class);
//						startActivity(intent);
						//										view.setBackgroundResource(R.drawable.shouchang1);
//						btn_queren.setEnabled(false);
					} else {
						Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		});
	}
	
	/**退单*/
	private void cancleorder() {
		SharedPreferences sp = getSharedPreferences("spReg",
				Context.MODE_PRIVATE);
		token = sp.getString("token", "");
		orderid = getIntent().getStringExtra("id");
//		TTs();
		if (httpTools == null) {
			httpTools = new HttpTools(OrderDetailActivity.this);
		}
		httpTools.cancleorder(token, orderid, new JsonCancleOrder());
	}
	
	private class JsonCancleOrder extends JsonHttpResponseHandler {
		@Override
		public void onSuccess(int statusCode, Header[] headers,
				JSONObject response) {
			super.onSuccess(statusCode, headers, response);
			try {
				boolean code = response.getBoolean("code");
				String msg = response.getString("msg");
				if (code == true) {
//					JSONObject object = response.getJSONObject("data");
					Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
					Intent intent = new Intent();
					intent.setClass(OrderDetailActivity.this, MainActivity.class);
					startActivity(intent);
//					dayin();
//					Log.i("lh", "printtype---"+"0");
				} else {
//					String msg = response.getString("msg");
					Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
//					dayindialog();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	
	
	/**是否取消订单弹窗*/
	private void cancledialog() {

		final Dialog dialog = new Dialog(OrderDetailActivity.this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

		iscatview = LayoutInflater.from(OrderDetailActivity.this).inflate(
				R.layout.dialog_phone, null);
		TextView tital = (TextView) iscatview.findViewById(R.id.tital);
//		TextView tv_phone = (TextView) iscatview.findViewById(R.id.tv_phone);
		TextView tv_no = (TextView) iscatview.findViewById(R.id.tv_no2);
		TextView tv_yes = (TextView) iscatview.findViewById(R.id.tv_yes2);
//		tv_phone.setText(phone);
//		tv_phone.setVisibility(View.GONE);
		tital.setText("是否取消该订单?");
		dialog.setContentView(iscatview);
		dialog.show();
		tv_no.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		tv_yes.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Log.e("lh", "iscat-----"+iscat);
				cancleorder();
				dialog.dismiss();
			}
		});
	}
	
	/**订单详情*/
	private void orderDetail() {
//		Log.i("zq", "token--"+token);
		//			TTs();
		if (httpTools == null) {
			httpTools = new HttpTools(OrderDetailActivity.this);
		}
		httpTools.detail(token, orderid, new JsonOrderDetail());
	}

	private class JsonOrderDetail extends JsonHttpResponseHandler {
		@Override
		public void onSuccess(int statusCode, Header[] headers,
				JSONObject response) {
			super.onSuccess(statusCode, headers, response);
			try {
				boolean code = response.getBoolean("code");
				if (code == true) {
					JSONObject object = response.getJSONObject("data");
					JSONArray array = object.getJSONArray("details");
					paymodeid = object.getString("paymodeid");
					state = object.getString("state");
					for (int i = 0; i < array.length(); i++) {
						JSONObject object2 = array.getJSONObject(i);
						String id = object2.getString("id");
						String name = object2.getString("name");
						String amount = object2.getString("amount");
						String price = object2.getString("price");
						String content = object2.getString("content");
						//							gonds.add(content+" - ");
					}
					if(state.equals("3")){
//						tv_cancle.setVisibility(View.GONE);
						btn_queren.setText("订单已完成");
						btn_queren.setEnabled(false);
					}else if(state.equals("1")){
//						tv_cancle.setVisibility(View.VISIBLE);
						btn_queren.setText("完成订单");
					}else{
//						tv_cancle.setVisibility(View.GONE);
						btn_queren.setText("完成订单");
					}
//					if(note.equals("")||note.equals("null")){
//						tv_content.setText("无");
//						note="无";
//					}else{
//						tv_content.setText(note);
//					}
//					if(recmode.equals("1")){
//						tv_fee.setVisibility(View.INVISIBLE);
//					}else if (recmode.equals("2")) {
//						tv_fee.setVisibility(View.VISIBLE);
//						tv_fee.setText("(含运费￥"+fee+")");
//					}
//					tv_shuliang.setText(""+shuliang+"份");
//					tv_zongjia.setText(""+heji+"元");

//					caidanAdapter = new CaidanAdapter(context, detaillist);
//					lv.setAdapter(caidanAdapter);
//					lv.setDivider(null);
//					Utility.setListViewHeightBasedOnChildren(lv);
					//							Log.i("zq", "list"+array.length());
				} else {
					String msg = response.getString("msg");
					Toast.makeText(OrderDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	
	

}